
package hasthi.metamodel.resource;


import hasthi.common.ChangeListener;
import hasthi.common.HasthiException;
import hasthi.common.InvaliedResourceDataException;
import hasthi.common.constants.WSDMConstants;
import hasthi.common.constants.WSDMConstants.SystemState;
import hasthi.manager.ManagedResourceType;
import static hasthi.common.constants.WSDMConstants.*;

import java.util.Map;

import javax.xml.namespace.QName;

/**
 * MetaModel of a Local ManagedResource, which means this is the primary copy of the metamodel. 
 * It includes support for a change listener, which can collect the changes happen over some time. 
 * This is created by the manager who manages the resource.
 * @author Srinath Perera(hperera@cs.indiana.edu)
 */

public class ManagedLocalResourceImpl extends AbstractManagedResource implements EditableManagedResource {
 

    
    ManagedLocalResourceImpl(ResourceModelContext modelContext,ManagedResourceType resourceDocument) throws InvaliedResourceDataException {
        super(modelContext,resourceDocument);
    }

    
    ManagedLocalResourceImpl(ResourceModelContext modelContext, Map<QName, Object> extractedProperties,String managerAgentURL) throws InvaliedResourceDataException {
        super(modelContext,extractedProperties,managerAgentURL);
    }

    ManagedLocalResourceImpl(QName name, ResourceModelContext modelContext, String managerAgentURL, 
            String category, String userDefinedType) throws InvaliedResourceDataException {
        super(name, modelContext, managerAgentURL, category, userDefinedType);
    }

    public void setProperty(String name,Object value) throws HasthiException{
        //We format the object value as a String
        String strValue = modelContext.getConverter().convert(value);
        String oldValue = properties.get(name);
        if(oldValue == null || !oldValue.equals(value)){
            properties.put(name, strValue);

            ChangeListener changeListener = modelContext.getChangeListener();
            if(changeListener != null){
                changeListener.propertyChanged(this.name, new QName(name), value);
            }
        }
    }


    @Override
    public String getProperty(String name) {
        return properties.get(name);
    }
    
    public void setState(SystemState state) throws HasthiException{
        String name = RESOURCE_STATUS_PARAM.getLocalPart(); 
        setProperty(name, state.toString());
    }
}

